package com.self.study.subjectsstudy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.self.study.subjectsstudy.common.ResultInfo;
import com.self.study.subjectsstudy.db.bean.Score;
import com.self.study.subjectsstudy.service.IScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lixunhuan
 * @since 2021-03-16
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private IScoreService scoreService;
    private static String AUTH_TOKEN = "Authorization";


    @RequestMapping("add_score")
    public ResultInfo addScore(HttpServletRequest httpServletRequest, @RequestParam("score") Integer score){
        Score scores = new Score();
        scores.setOpenid(httpServletRequest.getHeader(AUTH_TOKEN));
        scores.setScore(score);
        scoreService.save(scores);
        return new ResultInfo(ResultInfo.Status.SUCCESS).message("考试记录添加成功");
    }


    @RequestMapping("select_score")
    public ResultInfo selectScore(HttpServletRequest httpServletRequest){
        List<Score> scoreList= scoreService.list(new QueryWrapper<Score>().eq("openid",httpServletRequest.getHeader(AUTH_TOKEN)).orderByAsc("id"));
        return new ResultInfo(ResultInfo.Status.SUCCESS).result(scoreList);
    }

}
